package com.dc.event.listener.handler.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.pentaho.di.job.Job;

import com.dc.di.util.ServletInvokeUtil;
import com.dc.di.xml.XmlStringBuilder;
import com.dc.event.listener.handler.AbstractHandler;
import com.dc.event.listener.handler.JobStatusHandler;

public class ServletTransportHandler extends AbstractHandler implements JobStatusHandler {
	private  String url = new StringBuilder("http://").append(p.getProperty("ip")).append(":").append(p.getProperty("port")).append(p.getProperty("service")).toString();
	public static final String type = "parameter";
	@Override
	public void reciveJobStatusInfo(Job job,
			List<Map<String, String>> jobStatus) {
		XmlStringBuilder xml = getXml(job, jobStatus);
		send(xml);
	}
	
	private void send(XmlStringBuilder xml){
		logger.info("start to send jobstatus,target is "+url);
		
		String requestType =  p.getProperty("type");
		String requestCharset =  p.getProperty("charset");
		String parameterName =  p.getProperty("parameterName");
		
		try {
			if(requestType.equals(type)){
				Map<String, String> map = new HashMap<String, String>();
				map.put(parameterName, xml.toString());
				ServletInvokeUtil.doPost(url, map);
			}else{
				ServletInvokeUtil.postXML(url, xml.toString(), requestCharset);
			}
		} catch (Exception e) {
			logger.error("sending jobstatus has error:",e);
		}		
	}
}
